package com.atguigu.gmall.all.controller;



import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {
    @Autowired
    private ProductFeignClient productFeignClient;

    //首页  http://www.gmall.com/
    @GetMapping("/")
    public String index(Model model){
        List<Map> list = getData();
        model.addAttribute("list",list);
        return "index/index";//Springmvc 整合了Thymeleaf
    }

    private List<Map> getData() {
        List<Map> list = new ArrayList<>();
        //获取所有的视图集合
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        //根据一级id进行分类 Long 为1级id, List<BaseCategoryView> 为对应1级id的集合
        Map<Long, List<BaseCategoryView>> map1 = baseCategoryViewList.stream().collect(
                Collectors.groupingBy(baseCategoryView -> {
                    return baseCategoryView.getCategory1Id();
                })
        );
        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry1 : map1.entrySet()) {
            List<BaseCategoryView> value1 = longListEntry1.getValue();
            Map mapByCategory1Id = new HashMap();
            mapByCategory1Id.put("index", ++index);
            ///////////////////////////////////////////////////////////////
            List<Map> listMap2 = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> map2 = value1.stream().collect(
                    Collectors.groupingBy(BaseCategoryView::getCategory2Id)
            );
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntry2 : map2.entrySet()) {
                List<BaseCategoryView> value2 = longListEntry2.getValue();
                Map mapByCategory2Id = new HashMap();
                ///////////////////////////////////////////////////////////////
                List<Map> listMap3 = new ArrayList<>();
                Map<Long, List<BaseCategoryView>> map3 = value2.stream().collect(
                        Collectors.groupingBy(BaseCategoryView::getCategory3Id)
                );
                for (Map.Entry<Long, List<BaseCategoryView>> longListEntry3 : map3.entrySet()) {
                    Map mapByCategory3Id = new HashMap();
                    mapByCategory3Id.put("categoryId",longListEntry3.getKey());
                    mapByCategory3Id.put("categoryName",longListEntry3.getValue().get(0).getCategory3Name());
                    listMap3.add(mapByCategory3Id);
                }
                ///////////////////////////////////////////////////////////////
                mapByCategory2Id.put("categoryChild",listMap3);
                mapByCategory2Id.put("categoryId",longListEntry2.getKey());
                mapByCategory2Id.put("categoryName",longListEntry2.getValue().get(0).getCategory2Name());
                listMap2.add(mapByCategory2Id);
            }
            ///////////////////////////////////////////////////////////////
            mapByCategory1Id.put("categoryChild",listMap2);
            mapByCategory1Id.put("categoryId",longListEntry1.getKey());
            mapByCategory1Id.put("categoryName",longListEntry1.getValue().get(0).getCategory1Name());
            list.add(mapByCategory1Id);
        }
        return list;
    }

    @Autowired
    private TemplateEngine templateEngine;
    //人为触发 生成静态化的页面
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        //1.静态化页面过程

        //2.数据
        List<Map> list = getData();
        Context context = new Context();
        context.setVariable("list",list);
        //3.模板
        String templateName="index/index";
        //4.输出流
        Writer out = null;
        try {
            out = new PrintWriter("G:\\web\\temp\\index.html","UTF-8");
            //读
            templateEngine.process(templateName,context,out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return Result.ok();

    }
}
